﻿using FictionsDownload.BLL;
using FictionsDownload.DAL;
using FictionsDownload.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FictionsDownload.UI.PopDialog
{
    public partial class FormFilterDirectory : Form
    {
        string bookid;
        private IDAL db = db_Fictions.Create();
        private BLL.Directory directory = new BLL.Directory();
        public FormFilterDirectory(string id)
        {
            bookid = id;
            InitializeComponent();
            LvFilterDirectoryInit();
        }
        private void LvFilterDirectoryInit()
        {
            //列表样式
            LvFilterDirectory.View = View.Details;
            LvFilterDirectory.FullRowSelect = true;
            LvFilterDirectory.GridLines = true;
            //初始化列
            LvFilterDirectory.Columns.Add("Id", 60, HorizontalAlignment.Left);
            LvFilterDirectory.Columns.Add("Name", 80, HorizontalAlignment.Left);
            LvFilterDirectory.Columns.Add("Url", 220, HorizontalAlignment.Left);
            //加载内容
            LvFilterDirectoryRefresh();
        }
        private void LvFilterDirectoryRefresh()
        {
            var directorys = directory.GetList<DirectoryModel>(d => d.BookId == bookid && d.State == false);
            LvFilterDirectory.BeginUpdate();
            LvFilterDirectory.Items.Clear();
            foreach (var item in directorys)
            {
                ListViewItem viewItem = new ListViewItem();
                viewItem.Text = item.Id.ToString();
                viewItem.SubItems.Add(item.Name);
                viewItem.SubItems.Add(item.Url);
                LvFilterDirectory.Items.Add(viewItem);
            }
            LvFilterDirectory.EndUpdate();
        }
        private void BtnDelete_Click(object sender, EventArgs e)
        {
            var selectItems = LvFilterDirectory.SelectedItems;
            if (selectItems.Count > 0)
            {
                foreach (ListViewItem item in selectItems)
                {
                    db.Find<DirectoryModel>(item.Text).Delete(db);
                }
                db.SaveChanges();
                LvFilterDirectoryRefresh();
            }

        }

        private void BtnShow_Click(object sender, EventArgs e)
        {
            var selectItems = LvFilterDirectory.SelectedItems;
            if (selectItems.Count > 0)
            {
                foreach(ListViewItem item in selectItems)
                {
                    var directory = db.Find<DirectoryModel>(item.Text);
                        directory.State = true;
                    db.Update(directory);
                }
                db.SaveChanges();
                LvFilterDirectoryRefresh();
            }
        }
    }
}
